<template>
  <div style="width: 1200px; margin: 0 auto">
    <div style="margin-top: 20px;">
      <!-- 顶部面包屑标识与导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }">
          <i class="el-icon-s-promotion"></i> 全部
        </el-breadcrumb-item>
        <el-breadcrumb-item>{{ keyword }}</el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider></el-divider>
    </div>

    <!-- 商品列表 -->
    <div class="product_div" style="margin: 40px auto 40px 10px;overflow: hidden">
      <div style="margin:30px auto 0 auto;">
        <el-col :span="4" v-for="p in keyWordProductArr" :key="p.id"
                style="margin: 10px 0;width: 225px;margin-right: 10px">
          <el-card :body-style="{ padding: '0px'}">
            <el-image
                style="width: 200px;"
                :src="JSON.parse(p.pictures)[0].url"
                @click="loadProductDetails(p.id)"
                fit="fit"></el-image>
            <div style="padding: 14px;" @click="loadProductDetails(p.id)">
              <div class="bottom clearfix">
                <a href="" @click="loadProductDetails(p.id)">
                  <span class="product_title_span">{{ p.title }}</span></a>
              </div>
              <el-button type="text" class="button" style="line-height: 20px" @click="$router.puth('/')">
              </el-button>
              <p style="font-size: 16px">
                <b>￥{{ p.discountPrice }}</b> &nbsp; <s>{{ p.originalPrice }}</s>
                <span style="float: right;">销量：{{ p.sales }}件</span>
              </p>
            </div>
          </el-card>
        </el-col>
      </div>
    </div>

    <div id="search-results">
      <p>没有找到相关结果</p>
      <!--      <img src="../../../../public/images/index/dongman04.jpg" style="width: 1200px;position: relative;top: 4px">-->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      keyword: '',
      // results: [],
      keyWordProductArr: [],
    };
  },
  methods: {
    loadKeyword() {
      this.keyword = localStorage.getItem('keyword');
    },
    loadProductDetails(spuId) {
      this.$router.push('/sys-user/product/details?spuId=' + spuId);
    },
    loadSpuByKeyword() {
      let url = 'http://localhost:14001/spu/listByKeywords?keyword=' + this.keyword;
      console.log('url = ' + url);
      let localJwt = JSON.parse(localStorage.getItem('localJwt'));
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt.token
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.keyWordProductArr = jsonResult.data;
        }
      });
    },
  },
  mounted() {
    this.loadKeyword();
    this.loadSpuByKeyword();
  }
};
</script>

<style scoped>
#search-results {
  display: none;
}

.search-bar {
  display: flex;
  align-items: center;
  width: 300px;
}

.search-bar input {
  flex: 1;
  padding: 6px;
}

.search-bar button {
  padding: 6px;
}

.product_title_span {
  width: 200px;
  display: inline-block;
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis;
}
</style>